<?php

class Application_Model_DbTable_Answer extends Zend_Db_Table_Abstract
{

    protected $_name = 'ojanswer';

    public function addAnswer(Application_Model_Answer $answer){
        $data = array(
            'ojlId' => $answer->getLanguage()->getId(),
            'ojqId' => $answer->getQuestion()->getId(),
            'ojuId' => $answer->getUser()->getId(),
            'ojaCode' => $answer->getCode(),
            'ojaRunTime' => $answer->getRunTime(),
            'ojaRunMemory' => $answer->getRunMemory(),
            'ojaOutput' => $answer->getOutput(),
            'ojaTime' => $answer->getTime(),
            'ojaMemo' => $answer->getMemo()
       );
       $this->insert($data);
    }
    
    public function updateAnswer(Application_Model_Answer $answer){
        $data = array(
            'ojlId' => $answer->getLanguage()->getId(),
            'ojqId' => $answer->getQuestion()->getId(),
            'ojuId' => $answer->getUser()->getId(),
            'ojaCode' => $answer->getCode(),
            'ojaRunTime' => $answer->getRunTime(),
            'ojaRunMemory' => $answer->getRunMemory(),
            'ojaOutput' => $answer->getOutput(),
            'ojaTime' => $answer->getTime(),
            'ojaMemo' => $answer->getMemo()
            );
        $this->update($data, 'ojaId = '.$answer->getId());
    }
    
    public function getAnswerById($id){
        $row = $this->fetchRow('ojaId = '.$id);
        if (!$row) {
            return null;
        }
        $l = new Application_Model_DbTable_Language();
        $u = new Application_Model_DbTable_User();
        $q = new Application_Model_DbTable_Question();
        $answer = new Application_Model_Answer();
        $answer->setId($row->ojaId);
        $answer->setUser($u->getUserById($row->ojuId));
        $answer->setLanguage($l->getLanguageById($row->ojlId));
        $answer->setQuestion($q->getQuestionById($row->ojqId));
        $answer->setCode($row->ojaCode);
        $answer->setRunTime($row->ojaRunTime);
        $answer->setRunMemory($row->ojaRunMemory);
        $answer->setTime($row->ojaTime);
        $answer->setOutput($row->ojaOutput);
        $answer->setMemo($row->ojaMemo);
//        return $answer = new Application_Model_Answer($row->ojaId,
//                $u->getUserById($row->ojuId),
//                $l->getLanguageById($row->ojlId),
//                $q->getQuestionById($row->ojaId),
//                $row->ojaCode,$row->ojaRunTime,$row->ojaRumMemory,
//                $row->ojaTime,$row->ojaOutput,$row->ojaMemo
//                );
        return $answer;
    }
    
    public function deleteAnswer(int $id){
        $this->delete('ojaId = '.$id);
    }
    
    public function getAllAnswers(){
        $rowset = $this->fetchAll();
        $answers = array();
        $l = new Application_Model_DbTable_Language();
        $u = new Application_Model_DbTable_User();
        $q = new Application_Model_DbTable_Question();
        foreach ($rowset as $row) {
            $answer = new Application_Model_Answer();
            
            $answer->setId($row->ojaId);
            $answer->setUser($u->getUserById($row->ojuId));
            $answer->setLanguage($l->getLanguageById($row->ojlId));
            $answer->setQuestion($q->getQuestionById($row->ojqId));
            $answer->setCode($row->ojaCode);
            $answer->setRunTime($row->ojaRunTime);
            $answer->setRunMemory($row->ojaRunMemory);
            $answer->setTime($row->ojaTime);
            $answer->setOutput($row->ojaOutput);
            $answer->setMemo($row->ojaMemo);
            
            $answers[] = $answer;
        }
        return $answers;
    }
    
    public function getAnswersByUser(Application_Model_User $user){
        $rowset = $this->fetchAll('ojuId = '.$user->getId());
        $answers = array();
        $l = new Application_Model_DbTable_Language();
        $u = new Application_Model_DbTable_User();
        $uu = $u->getUserById($user->getId());
        $q = new Application_Model_DbTable_Question();
        foreach ($rowset as $row) {
            
            $answer = new Application_Model_Answer();
            
            $answer->setId($row->ojaId);
            $answer->setUser($uu);
            $answer->setLanguage($l->getLanguageById($row->ojlId));
            $answer->setQuestion($q->getQuestionById($row->ojqId));
            $answer->setCode($row->ojaCode);
            $answer->setRunTime($row->ojaRunTime);
            $answer->setRunMemory($row->ojaRunMemory);
            $answer->setTime($row->ojaTime);
            $answer->setOutput($row->ojaOutput);
            $answer->setMemo($row->ojaMemo);
            
            $answers[] = $answer;
        }
        return $answers;
    }
    
    public function getAnswersByQuestion(Application_Model_Question $question){
        $rowset = $this->fetchAll('ojqId = '.$question->getId());
        $answers = array();
        $l = new Application_Model_DbTable_Language();
        $u = new Application_Model_DbTable_User();
        $q = new Application_Model_DbTable_Question();
        $qq = $q->getQuestionById($question->getId());
        foreach ($rowset as $row) {
            $answer = new Application_Model_Answer($row->ojaId,
                $u->getUserById($row->ojuId),
                $l->getLanguageById($row->ojlId),
                $qq,
                $row->ojaCode,$row->ojaRunTime,$row->ojaRumMemory,
                $row->ojaTime,$row->ojaOutput,$row->ojaMemo
             );
            $answers[] = $answer;
        }
        return $answers;
    }
    
    public function getAnswersByLanguage(Application_Model_Language $language){
        $rowset = $this->fetchAll('ojlId = '.$language->getId());
        $answers = array();
        $l = new Application_Model_DbTable_Language();
        $ll = $l->getLanguageById($language->getId);
        $u = new Application_Model_DbTable_User();
        $q = new Application_Model_DbTable_Question();
        foreach ($rowset as $row) {
            $answer = new Application_Model_Answer($row->ojaId,
                $u->getUserById($row->ojuId),
                $ll,
                $q->getQuestionById($row->ojaId),
                $row->ojaCode,$row->ojaRunTime,$row->ojaRumMemory,
                $row->ojaTime,$row->ojaOutput,$row->ojaMemo
             );
            $answers[] = $answer;
        }
        return $answers;
    }
    
    public function getAnswersByRange($start,$offset){
        $rowset = $this->fetchAll(null,'ojaId',$offset,$start);
        $answers = array();
        $l = new Application_Model_DbTable_Language();
        $u = new Application_Model_DbTable_User();
        $q = new Application_Model_DbTable_Question();
        foreach ($rowset as $row) {
            $answer = new Application_Model_Answer();
            
            $answer->setId($row->ojaId);
            $answer->setUser($u->getUserById($row->ojuId));
            $answer->setLanguage($l->getLanguageById($row->ojlId));
            $answer->setQuestion($q->getQuestionById($row->ojqId));
            $answer->setCode($row->ojaCode);
            $answer->setRunTime($row->ojaRunTime);
            $answer->setRunMemory($row->ojaRunMemory);
            $answer->setTime($row->ojaTime);
            $answer->setOutput($row->ojaOutput);
            $answer->setMemo($row->ojaMemo);
            
            $answers[] = $answer;
        }
        return $answers;
    }
    
    public function countAnswers() {
        $select = $this->select();
        $select->from($this->_name,'COUNT(ojaId) AS num');
        return $this->fetchRow($select)->num;
    }
    
    //查询某用户是否有同一题同一语言的成功提交
    public function getOnesAnswersByQuestion(Application_Model_User $user, Application_Model_Question $question, 
            Application_Model_Language $language){
        $select = $this->select();
        $select->where('ojuId = '.$user->getId());
        $select->where('ojqId = '.$question->getId());
        $select->where('ojlId = '.$language->getId());
        $rowset = $this->fetchAll($select);
        
        if(!$rowset)
            return null;
        
        foreach($rowset as $row)
        {
            if($row->ojaMemo == '$10000001')
            {
                $l = new Application_Model_DbTable_Language();
                $u = new Application_Model_DbTable_User();
                $uu = $u->getUserById($user->getId());
                $q = new Application_Model_DbTable_Question();
                $answer = new Application_Model_Answer();

                $answer->setId($row->ojaId);
                $answer->setUser($uu);
                $answer->setLanguage($l->getLanguageById($row->ojlId));
                $answer->setQuestion($q->getQuestionById($row->ojqId));
                $answer->setCode($row->ojaCode);
                $answer->setRunTime($row->ojaRunTime);
                $answer->setRunMemory($row->ojaRunMemory);
                $answer->setTime($row->ojaTime);
                $answer->setOutput($row->ojaOutput);
                $answer->setMemo($row->ojaMemo);
                
                return $answer;
            }
        }
    }
    
}


